/**
* <p>Title: HtmlUnit.java</p>
* <p>Description: </p>
* <p>Company: www.huoyunren.com</p>
* @author duanxiangyun
* @date 2015年5月14日
* @version 1.0
*/
package com.hxbd.test.utils.webdriver;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import com.duan.test.utils.Excel.DefaultExcelParser;
import com.duan.test.utils.Excel.IExcelParser;
import com.duan.test.utils.common.CommonUtils;


public class HtmlUnit {
		/**
		 * 将html文件反转义
		 * @param path 路径
		 * @param inFile 文件名——入
		 * @param outFile 文件名——出
		 */
		public static void createHtml(String path, String inFile, String outFile){
			try{
				BufferedReader br;
				BufferedWriter bw;
				String tempStr = "";
				try {
					br = new BufferedReader(new InputStreamReader(new FileInputStream(path + inFile),"UTF-8")); 
					bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(path + outFile),"UTF-8"));
					while ((tempStr = br.readLine()) != null) {
						System.out.println(tempStr);
						tempStr = tempStr.replaceAll("&lt;", "<");
						tempStr = tempStr.replaceAll("&quot;", "\"");
						tempStr = tempStr.replaceAll("&gt;", ">");
						tempStr = tempStr.replaceAll("&apos;", "'");					
						bw.write(tempStr);
						bw.write("\r\n");
					}
					bw.flush();
					br.close();
					bw.close();
				} catch (FileNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			catch(Exception e){
				System.out.print(e.toString());
			}
		}
		//替换extent报告css/js
		public static void createNewExtentHtml(String path, String inFile, String outFile){
			try{
				BufferedReader br;
				BufferedWriter bw;
				String tempStr = "";
				try {
					br = new BufferedReader(new InputStreamReader(new FileInputStream(path + inFile),"UTF-8")); 
					bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(path + outFile),"UTF-8"));
					while ((tempStr = br.readLine()) != null) {
						tempStr = tempStr.replaceAll("https://cdn.rawgit.com/anshooarora/extentreports-java/6d3843c29c354373fafad29c03a1a89b7e06c374/dist/css/extent.css", "");
						tempStr = tempStr.replaceAll("https://cdn.rawgit.com/anshooarora/extentreports-java/5ed7b0c339b51aa347b44ccaa2e2ff29905b2f9f/dist/js/extent.js", "");					
						bw.write(tempStr);
						bw.write("\r\n");
					}
					bw.flush();
					br.close();
					bw.close();
				} catch (FileNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			catch(Exception e){
				System.out.print(e.toString());
			}
		}
		public static void reReadHtmlFile (String path, String inFile, String outFile) {
			try{
				BufferedReader br;
				BufferedWriter bw;
				String tempStr = "";
				String str = "";
				try {
					br = new BufferedReader(new InputStreamReader(new FileInputStream("C:\\Users\\Huoyunren\\Desktop\\emailable-report.html"),"UTF-8")); 
					bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(path + outFile),"UTF-8"));
					while ((str = br.readLine()) != null) {
//						tempStr = tempStr.replaceAll("<td><a href=\"#m\\d*\">[a-zA-Z0-9_]+</a></td><td rowspan=\"\\d*\">\\d{13}</td><td rowspan=\"\\d*\">\\d*</td>", "");					
//						tempStr = tempStr.replaceAll("<td><a href=\"#m\\d*\">[a-zA-Z0-9_]+</a></td>", "");						
//						tempStr = tempStr.replaceAll("<th>Class</th><th>Method</th><th>Start</th><th>Time (ms)</th>", "<th>测试模块</th><th>测试点 </th><th>失败原因</th>");
						tempStr = tempStr + str;
//						bw.write(tempStr);
//						bw.write("\r\n");
					}
					tempStr = tempStr.replaceAll("<td><a href=\"#m\\d*\">[a-zA-Z0-9_]+</a></td><td rowspan=\"\\d*\">\\d{13}</td><td rowspan=\"\\d*\">\\d*</td>", "");					
					tempStr = tempStr.replaceAll("<td><a href=\"#m\\d*\">[a-zA-Z0-9_]+</a></td>", "");						
					tempStr = tempStr.replaceAll("<th>Class</th><th>Method</th><th>Start</th><th>Time \\(ms\\)</th>", "<th>测试模块</th><th>测试点 </th><th>失败原因</th>");
					tempStr = tempStr.replaceAll("testIntelligentTubeCar", "智能管车");
					IExcelParser ie = DefaultExcelParser.createExcelParser("C:\\Users\\Huoyunren\\Desktop\\自动化脚本覆盖功能点.xlsx");
					Sheet sh = ie.getSheet(ie.getWorkBook(), 0);
					for (int i = 0; i <= sh.getLastRowNum(); i++) {
						String tmpvalue = ie.getCellValue(sh, i, 0);
						System.out.println(tempStr);
		            	if (!CommonUtils.isStrEmpty(tmpvalue) && tempStr.contains(tmpvalue)) {
		            		String aa = ie.getCellValue(sh, i, 1) + "</td><td>" + ie.getCellValue(sh, i, 2);
							tempStr = tempStr.replaceAll(tmpvalue, aa);
		            	}
					}
					bw.write(tempStr);
					bw.flush();
					br.close();
					bw.close();
				} catch (FileNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			catch(Exception e){
				System.out.print(e.toString());
			}
		}
		/**
		 * 文件重命名
		 * @param path 文件目录 
		 * @param oldname  原来的文件名 
	     * @param newname 新文件名 
		 */
		public static void renameFile(String path,String oldname,String newname) { 
	        if(!oldname.equals(newname)){//新的文件名和以前文件名不同时,才有必要进行重命名 
	            File oldfile=new File(path + oldname); 
	            File newfile=new File(path + newname); 
	            if(!oldfile.exists()){
	                return;//重命名文件不存在
	            }
	            if(newfile.exists())//若在该目录下已经有一个文件和新文件名相同，则不允许重命名 
	                System.out.println(newname+"已经存在！"); 
	            else{ 
	                oldfile.renameTo(newfile); 
	            } 
	        }else{
	            System.out.println("新文件名和旧文件名相同...");
	        }
		}
		/**
		 * 获得新html文件
		 * @param path 路径
		 * @param inFile 文件名——入
		 * @param outFile 文件名——出
		 */
		public static void getNewHtml (String path, String inFile, String outFile) {
			createHtml(path, inFile, outFile);
			File file = new File(path + inFile);
			if (file.exists()) {
				file.delete();
			}
			renameFile(path, outFile, inFile);
			try {
				Thread.sleep(5000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
}
